package com.koron.operation.classes.schedule.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.operation.classes.schedule.bean.po.ClassesStaff;
import com.koron.operation.classes.schedule.mapper.ClassesStaffMapper;
import com.koron.operation.classes.schedule.service.ClassesStaffService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 值班人员(ClassesStaff)表服务实现类
 *
 * @author jzp
 * @since 2021-10-20 16:20:37
 */
@Service
public class ClassesStaffServiceImpl implements ClassesStaffService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ClassesStaff getById(SessionFactory factory, String id) {
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ClassesStaff> list(SessionFactory factory, ClassesStaff classesStaff) {
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.selectList(classesStaff);
    }

    @Override
    @TaskAnnotation("listByShiftId")
    public List<ClassesStaff> listByShiftId(SessionFactory factory, String shiftId) {
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setShiftId(shiftId);
        return list(factory, classesStaff);
    }

    @Override
    @TaskAnnotation("listByShiftIdsAndTypes")
    public List<ClassesStaff> listByShiftIdsAndTypes(SessionFactory factory, List<String> shiftIds, List<String> types) {
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.listByShiftIdsAndTypes(shiftIds, types);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ClassesStaff classesStaff) {
        Assert.hasText(classesStaff.getStaffBy(), "人员账号不能为空");
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.insert(classesStaff) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ClassesStaff> classesStaffs) {
        if (CollectionUtils.isEmpty(classesStaffs)) {
            return false;
        }
        for (ClassesStaff staff : classesStaffs) {
            Assert.hasText(staff.getStaffBy(), "人员账号不能为空");
        }

        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.insertOrUpdateBatch(classesStaffs) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ClassesStaff classesStaff) {
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.update(classesStaff) > 0;
    }

    @Override
    @TaskAnnotation("sign")
    public boolean sign(SessionFactory factory, String id) {
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setId(id);
        classesStaff.setSigned(1);
        return update(factory, classesStaff);
    }

    @Override
    @TaskAnnotation("unSign")
    public boolean unSign(SessionFactory factory, String id) {
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setId(id);
        classesStaff.setSigned(0);
        return update(factory, classesStaff);
    }

    @Override
    @TaskAnnotation("clearSign")
    public boolean clearSign(SessionFactory factory, String shiftId) {
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setShiftId(shiftId);
        classesStaff.setSigned(0);
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.updateByShiftId(classesStaff) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ClassesStaffMapper mapper = factory.getMapper(ClassesStaffMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByScheduleId")
    public boolean removeByScheduleId(SessionFactory factory, String scheduleId) {
        if (StrUtil.isEmpty(scheduleId)) {
            return false;
        }
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setScheduleId(scheduleId);
        List<ClassesStaff> list = list(factory, classesStaff);

        return deleteBatchIds(factory, list.stream().map(ClassesStaff::getId).collect(Collectors.toList()));
    }

    @Override
    @TaskAnnotation("removeByShiftId")
    public boolean removeByShiftId(SessionFactory factory, String shiftId) {
        if (StrUtil.isEmpty(shiftId)) {
            return false;
        }
        ClassesStaff classesStaff = new ClassesStaff();
        classesStaff.setShiftId(shiftId);
        List<ClassesStaff> list = list(factory, classesStaff);

        return deleteBatchIds(factory, list.stream().map(ClassesStaff::getId).collect(Collectors.toList()));
    }
}
